//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


#include "Particle.h"
#include "Images/Image.h"


//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
CParticle::CParticle() {

  m_nTexture = 0;
}


//-----------------------------------------------------------------------------
// destructor
//-----------------------------------------------------------------------------
CParticle::~CParticle(){

  if (m_nTexture) {

    glDeleteTextures(1, &m_nTexture);
    m_nTexture = 0;
  }
}


//-----------------------------------------------------------------------------
// Initialize the particle
//-----------------------------------------------------------------------------
bool CParticle::Init(const char *pszFilename) {

  //aux vars
  CImage oTexture;
  GLenum eTexFormat;

  if (m_nTexture) {

    glDeleteTextures(1, &m_nTexture);
    m_nTexture = 0;
  }

  //load texture
  oTexture.Init();
  if (!oTexture.Load(pszFilename))
    return false;

  switch (oTexture.GetChannels()) {

    case 1:
      eTexFormat = GL_ALPHA;
      break;

    case 2:
      eTexFormat = GL_LUMINANCE_ALPHA;
      break;

    case 3:
      eTexFormat = GL_RGB;
      break;

    case 4:
      eTexFormat = GL_RGBA;
      break;

    default:
      cerr << "ERROR: could not find the PNG image buffer format: " << oTexture.GetChannels() << endl;
  }

  glGenTextures(1, &m_nTexture);
  glBindTexture(GL_TEXTURE_2D, m_nTexture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, oTexture.GetWidth(), oTexture.GetHeight(), 0, eTexFormat, GL_UNSIGNED_BYTE, oTexture.GetImage());

  if (glGetError() != GL_NO_ERROR) {

    cerr << "ERROR: OGL error loading terrain texture!" << endl;
    exit(0);
  }

  return true;
}


//-----------------------------------------------------------------------------
// draw the particle
//-----------------------------------------------------------------------------
void CParticle::Draw(float fScale) {

  //aux vars
  double afMatrix[16];

  glPushMatrix();
  glPushAttrib(GL_ALL_ATTRIB_BITS);

  glDisable(GL_ALPHA_TEST);
  glDisable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glDisable(GL_CULL_FACE);
  glDisable(GL_LIGHTING);
  glEnable(GL_TEXTURE_2D);

  glGetDoublev(GL_MODELVIEW_MATRIX, afMatrix);
  afMatrix[0]  = 1.0;
  afMatrix[1]  = 0.0;
  afMatrix[2]  = 0.0;
  afMatrix[4]  = 0.0;
  afMatrix[5]  = 1.0;
  afMatrix[6]  = 0.0;
  afMatrix[8]  = 0.0;
  afMatrix[9]  = 0.0;
  afMatrix[10] = 1.0;
  glLoadMatrixd(afMatrix);

  glBindTexture(GL_TEXTURE_2D, m_nTexture);
  glScalef(fScale, fScale, fScale);

  glBegin(GL_QUADS);

    glTexCoord2f(0, 1);
    glVertex2f(-0.5f,  0.5f);

    glTexCoord2f(1, 1);
    glVertex2f( 0.5f,  0.5f);

    glTexCoord2f(1, 0);
    glVertex2f( 0.5f, -0.5f);

    glTexCoord2f(0, 0);
    glVertex2f(-0.5f, -0.5f);

  glEnd();

  glPopAttrib();
  glPopMatrix();
}
